/*
 * Created on 2005-3-22
 * Copyright: Bluejay Networks Inc. 2004
 *
 * This software is the proprietary information of Bluejay Networks Inc. & 
 * Inquiry Management Systems Ltd.  
 * Use is subject to license terms.
 * 
 */
package com.ims.iaa.AD;

import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Timestamp;
import java.util.List;

import com.ims.iaa.IaaUtil;

/**
 * @author changsheng gong
 * @version 1.00, 22-3-2005
 * This class represents href link data collected by spiders. 
 */
public class HrefLink extends AdDataLink{

	private URL m_objTargetUrl;// the <A href=> ;
	private URL m_objRealTargetUrl;// the real target url after connect to the target
	private URL m_objContainerUrl;		// the url in which this href link was contained
	
	private String m_sUserAgent;
	private String m_sAdvertiser;
	private Timestamp m_objCollectTime;
	private int m_iSrcReqStatus;
	//because we don't use it and the UCConnection of HrefParser is no Serialize
	private transient HrefParser m_hrefParser;
	
	//construct for new HrefLink
	public HrefLink(HrefParser hrefParser, URL container) throws MalformedURLException {
	    m_hrefParser = hrefParser;
        m_objTargetUrl = m_hrefParser.getOriginalURL();
		m_objContainerUrl = container;
    }

    public String getAdType() {
        return ADTYPE_AGENCY;
    }
    public AdData getAdData() {
        return null;
    }

    public int getHeight() {  
        return 0;
    }

    public void setHeight(int iHeight) {
    }

    public int getWidth() {    
        return 0;
    }

    public void setWidth(int iWidth) {
       
    }
    public void setUserAgent(String agent) {
        m_sUserAgent = agent;
    }

    public URL getSrcURL() {
       return null;
    }

    public String getSrcURLStr() {
       return null;
    }

    public String getAdDataSite() {
       return null;
    }

    public URL getContainerURL() {
       return m_objContainerUrl ;
    }

    public URL getTargetURL() {
        return m_objTargetUrl;
    }

    public String getTargetURLStr() {
        return m_objTargetUrl.toString();
    }

    public String getRealTargetURLStr() {
        if(m_objRealTargetUrl == null){
            return null;
        }
        return  m_objRealTargetUrl.toString();
    }

    public String getAdvertiser() {
        return m_sAdvertiser;
    }

    public Timestamp getCollectTime() {
        return m_objCollectTime; 
    }

    public boolean parse(List lstAgency,List lstAdSize) { 
        try{
    		incPossibility(100);
            setAdAgency(m_hrefParser.getAgency());
            if(m_hrefParser.connect()){
		        m_objRealTargetUrl = m_hrefParser.getRedirectURL();
		        m_sAdvertiser = m_objRealTargetUrl.getHost();
		        m_objCollectTime = new Timestamp(  System.currentTimeMillis() );
	            m_iSrcReqStatus = CONN_SUCCESS;
            }else{
                m_iSrcReqStatus = CONN_FAILURE;
            }
        }catch(Exception e){
            m_iSrcReqStatus = CONN_FAILURE;
            IaaUtil.printStackTrace(e);
        }
        return true;
    }
    
    public boolean isConnectSuccessful() {
        if(m_iSrcReqStatus == CONN_SUCCESS){
			return true;
		}else{
			return false;
		}
    }
	
	public String toString(){
	    return "HREF: " + m_objTargetUrl.toString();
	}
}
